package operator.create;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import util.Printer;
import util.ThreadHelper;

class TimerOperator {

    public static void main(String[] args) {
        timer();
    }

    static void timer(){
        //指定时间之后发送，默认在子线程上,延迟指定时间后，发送1个值为0的Long类型对象
        Observable.timer(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Printer.printThreadName();
                        Printer.print(String.valueOf(aLong));
                    }
                });
        Printer.print("after timer1");
        //AndroidSchedulers.mainThread()
        Observable.timer(2, TimeUnit.SECONDS, Schedulers.io())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Printer.printThreadName();
                        Printer.print(String.valueOf(aLong));
                    }
                });
        Printer.print("after timer2");
        ThreadHelper.sleep();
    }

}
